package com.qcby.byc.demo0422.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.byc.demo0422.entity.Person;
import com.qcby.byc.demo0422.service.PersonService;
import com.qcby.byc.demo0422.util.PageVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @className: PersonController
 * @description:
 * @description:
 * @author: lxt
 * @create: 2021-04-22 17:19
 **/
@RestController
@RequestMapping("person")
public class PersonController {
    //
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PersonService personService;


    @RequestMapping("add")
    @ResponseBody
    public Person addDemo(Person person){

        return person;
    }


    @GetMapping("listAll")
    public PageVo listAll(int pageNum, int pageSize){
        return personService.listAll(pageNum,pageSize);
    }



    @GetMapping("listAllNew")
    public IPage<Person> listAll(int pageNo, Integer pageSize, String name){
        Page<Person> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        return personService.page(page,queryWrapper);
    }

    /**
     *   主键：1387970771024678913
     * @param person
     * @return
     */
    @GetMapping("add")
    public boolean add(Person person){
        return personService.saveOrUpdate(person);
    }


    @GetMapping("listAll1")
    public List<Person> listAll1(String name){
        logger.info("name-info:"+name);
        // 占位符也是推荐使用的
        logger.info("name-info:{}-{}-{}",name,"name2","name3");

        logger.warn("name-warn:"+name);
        logger.warn("name-warn:{}",name);


        logger.error("name-error:"+name);
        logger.error("name-error:{}",name);

        List<Person> personList = personService.listAll1();
        return personList;
    }


}
